
#include <pic18.h>
#include "RF12B.h"
#include "my_macros.h"
#include "UART.h"
#include "App.h"
#include "String.h"
#include "SysClock.h"
#include "printf.h"
#include "Command.h"
#include "mem.h"

#if defined(_16F886) || defined(_18F6722) || defined(_18F25K20)
#define  RF_CS				RA5
#define  RF_CS_DIR		TRISA5

#if defined(HARDWARE__SH_CPU)
#define 	RF_READY			RB0//RC2	  	
#define 	RF_READY_DIR	TRISB0//RB0//RC2	  	
#else
#define 	RF_READY			RC1//RB0//RC2	  	
#define 	RF_READY_DIR	TRISC1//RB0//RC2	  	
#endif

#define  SPI_SCK_DIR		TRISC3			 
#define  SPI_SDO_DIR		TRISC5			 

#elif defined(_16F690)
#define  RF_CS				RC3
#define  RF_CS_DIR		TRISC3
#define 	RF_READY			RC2//RB0//RC2	  	
#define 	RF_READY_DIR	TRISC2//RB0//RC2	  	
#endif


unsigned int RF_Status = 0;


unsigned char SPI_SendByte( unsigned char Byte )
{
	unsigned char RecByte;

	RecByte = SSPBUF;
	SSPBUF = Byte;

	while( !BF )
		continue;

	NOP();
	
	RecByte = SSPBUF;
	
	return RecByte;
}	

unsigned int RF_SendCommand ( unsigned int Command )
{
	unsigned int rd;
	RF_CS = 0;
	NOP();
	rd = SPI_SendByte( Command>>8 ); 
	rd <<= 8;
	rd |= SPI_SendByte( Command & 0xFF ); 
	NOP();
	RF_CS = 1;
	return rd;
}
 
void RF_SendDataByte ( unsigned char DataByte )
{
	//while( !(RF_SendCommand(0x7FFF) & 0x8000 ) )LED2 = 1; /* read status */ 
	RF_CS = 0;
	NOP();
	while( RF_READY )
		continue;
	SPI_SendByte( WRITE_TO_TRANSMIT ); 
	SPI_SendByte( DataByte ); 
	//delay_us(10);
	RF_CS = 1;
}

void RF_ResetFIFO ( void )
{
	RF_SendCommand(RESET_FIFO);
	RF_SendCommand(SET_FIFO);
}

void RF_TXD( const unsigned char* Buffer)	  /* put the Data into TransmitBuffer and get the data from ReceiveBuffer */
{
	unsigned char i; //, s[10];

	RF_SendCommand(OPEN_PA);
	delay_us(60);

	RF_Status = RF_SendCommand(GET_STATUS);  /* activation nIRQ pin */
	RF_Status = RF_SendCommand(GET_STATUS);  /* activation nIRQ pin */

	RF_SendCommand(TRANSMIT_ENA);

	/* Preamble */
	RF_SendDataByte(0xAA);
	RF_SendDataByte(0xAA);
	RF_SendDataByte(0xAA);
	RF_SendDataByte(0xAA);
	
//	RF_SendCommand(TRANSMIT_ENA);

	/* Sync. word */
	RF_SendDataByte(0x2D);
	RF_SendDataByte(0xD4);

/* 	/\* Send ID number *\/ */
/* 	HexToChar(s, (unsigned char*)&Keypad_ID, sizeof(Keypad_ID)); */
/* 	i = 0; */
/* 	while( s[i] ) */
/* 	{ */
/* 		RF_SendDataByte(s[i++]); */
/* 	} */
	
//	while(++i);
	
	i = 0;
	while( Buffer[i])
	{
		//putchar(Buffer[i]);
		RF_SendDataByte( Buffer[i++] );		
	}

	RF_SendDataByte(0xAA); 		  /* dummy byte */
	RF_SendDataByte(0xAA); 		  /* dummy byte */
//	RF_SendDataByte(0xAA); 		  /* dummy byte */

	RF_SendCommand(CLOSE_PA);

	RF_SendCommand(RECEIVE_ENA);
}
  
unsigned char RF_Read_FIFO( void )
{
	unsigned char fifo;
	RF_CS = 0;
//	NOP();
	SPI_SendByte(0);
	SPI_SendByte(0);
	fifo = SPI_SendByte(0);
//	delay_us(50);
	RF_CS = 1;
//	NOP();
	return fifo;
}	


//bank1 unsigned char ReceiveBuffer[RECEIVE_BUFFER_SIZE];
bit RF_ReceiveComplete;
bit RF_Receive_Command;
bit RF_Receive_MyID;
bit RF_Receive_Broadcast;
bit RF_Receive_Answer;

RF_DATA RF_DataReceived;

unsigned char* RF_ReceiveBuffer = (unsigned char*)&RF_DataReceived;
unsigned char pRF_ReceiveBuffer = 0;



void RF_RXD ( void )
{
 
	RF_ReceiveBuffer[pRF_ReceiveBuffer] = RF_Read_FIFO();
	
	//putchar(RF_ReceiveBuffer[pRF_ReceiveBuffer]);

	if( RF_ReceiveBuffer[pRF_ReceiveBuffer] == '\r' && pRF_ReceiveBuffer >= 18 )
	{
		RF_ReceiveBuffer[++pRF_ReceiveBuffer] = 0;
		
		pRF_ReceiveBuffer = 0;
		
		RF_ReceiveComplete = TRUE;
		
		RF_ResetFIFO();
	}
	else
	{
		if( RF_ReceiveBuffer[pRF_ReceiveBuffer] < ' ' || RF_ReceiveBuffer[pRF_ReceiveBuffer] > 127 )
		{
			RF_ResetFIFO();
			pRF_ReceiveBuffer = 0;
		}
		else
			if ( ++pRF_ReceiveBuffer >= RECEIVE_BUFFER_SIZE-1 )
			{
				RF_ResetFIFO();
				pRF_ReceiveBuffer = 0;
			}
	}
}

/* void RF_RXD_2 ( void ) */
/* { */
/* 	//unsigned char Res; */
/* 	//RF_ReceiveComplete = FALSE; */

/* 	//RF_SendCommand(RECEIVE_ENA); */
/* 	//delay_us(10); */
	
/* 	//RF_SendCommand(GET_STATUS); */

/* 	//Res = RF_SendCommand(READ_FIFO);  */
/* 	//Res = RF_Read_FIFO();  */
	
/* 	RF_ReceiveBuffer[pRF_ReceiveBuffer] = RF_Read_FIFO(); */

/* 	//if( RF_ReceiveBuffer[pRF_ReceiveBuffer] == '\r') */
/* 	if( RF_ReceiveBuffer[pRF_ReceiveBuffer] == '\t') */
/* 	{ */

/* 		if ( pRF_ReceiveBuffer == 35 ) */
/* 		{ */
/* 			RF_ReceiveComplete = TRUE; */
/* 		} */

/* 		RF_ReceiveBuffer[pRF_ReceiveBuffer] = 0; */
/* 		pRF_ReceiveBuffer = 0; */

/* 		RF_ResetFIFO(); */
/* 	} */
/* 	else */
/* 	{ */
/* 		if( RF_ReceiveBuffer[pRF_ReceiveBuffer] == 0 ) */
/* 		{ */
/* 			pRF_ReceiveBuffer = 0; */
/* 		} */
/* 		else */
/* 			if ( ++pRF_ReceiveBuffer >= RECEIVE_BUFFER_SIZE ) */
/* 			{ */
/* 				pRF_ReceiveBuffer = 0; */
/* 			} */
/* 	} */
/* } */
void RF_Init ( void )
{
/* 	TRISC7 = OUTPUT; */
/* 	TRISB6 = OUTPUT; */

	
	/* SPI */
	SPI_SCK_DIR = OUTPUT;
	SPI_SDO_DIR = OUTPUT;

	SSPEN = 1;							  /* 1 = Enables serial port and configures SCK, SDO and SDI as serial port pins */
	CKP = 0;							  /*  */
	SMP = 0;
	CKE = 1;
	SSPCON1 |= 2;
	SSPOV = 1;
	SSPBUF = SSPBUF;

	
	RF_CS_DIR = OUTPUT;
	RF_CS = 1;
	RF_READY_DIR = INPUT;

	delay_ms(10);

	RF_SendCommand(BAND);		 		
	RF_SendCommand(TURN_ON_CRYSTAL);
	RF_SendCommand(FREQUENCY);
	RF_SendCommand(BIT_RATE);			
	RF_SendCommand(REC_CNTRL);		
	RF_SendCommand(DATA_FILTER);		
	RF_SendCommand(RESET_FIFO);		
	RF_SendCommand(SET_FIFO);			
	RF_SendCommand(AFC);				
	RF_SendCommand(DEV_POW);		
	RF_SendCommand(PLL);		
	RF_SendCommand(NOP_);				
	RF_SendCommand(DUTY_CYCLE);		
	RF_SendCommand(SPI_CLK_BAT_DET);
	

	delay_ms(50);
	RF_SendCommand(RECEIVE_ENA);
	
}

unsigned char RF_TimeoutCnt;

void Drive_RF ( void )
{
	unsigned long Rec_ID_Number;
	unsigned char MasterFound;

	RF_ReceiveComplete = FALSE;
	RF_Receive_Answer = FALSE;
	RF_Receive_Command = FALSE;
	RF_Receive_MyID = FALSE;
	RF_Receive_Broadcast = FALSE;


	if ( !RF_READY )
	{
		//putchar('F');
		RF_TimeoutCnt = 0;
		RF_RXD();
		
		/* Commnad Source RF */
		if ( RF_ReceiveComplete )
		{
			//LED = 1;
			DBG("\r\nRF_LISTEN: %s\r\n", RF_ReceiveBuffer);
			//LED = 0;
			CharnToHex((unsigned char*)&Rec_ID_Number, RF_DataReceived.Destination_ID, sizeof(Rec_ID_Number));

			if ( Rec_ID_Number == 0 ) /* broadcast */
			{
				RF_Receive_Broadcast = TRUE;
			}
			else
				if ( Rec_ID_Number == SerNum )
				{
					RF_Receive_MyID = TRUE;
				}

			
			/* check if command received from your Master */
			MASTER_DATA Master_Data;
			CharnToHex((unsigned char*)&Master_Data.Address, RF_DataReceived.Source_ID, 4);	
			MasterFound = emem_FindMaster(&Master_Data); 
		
			if ( (RF_Receive_Broadcast && App_Mode != APP_MODE__NORMAL) || RF_Receive_MyID )	
			{
				printf("RF: %s\r\n", RF_ReceiveBuffer);
								
				if ( StrFindChar((unsigned char*)&RF_DataReceived.RF_Command[2], ':') != -1 )
				{
					RF_Receive_Answer = TRUE;
				}
				else
				{
					Cmd_Add(RF_DataReceived.RF_Command, CMD_SOURCE__RF_CHANNEL);
					RF_Receive_Command = TRUE;				
				}

			}

			//if ( RF_Receive_MyID && RF_Receive_Command && MasterFound == FALSE )
			if ( MasterFound == FALSE )
			{
				DBG("\nMASTER_ERROR: %s", RF_DataReceived.Source_ID);
			}
		}		
	}
	else
		if ( pRF_ReceiveBuffer && Time_10ms && ++RF_TimeoutCnt > 1 )
		{
			RF_TimeoutCnt = 0;
			RF_ResetFIFO();
			pRF_ReceiveBuffer = 0;	  /* reset buffer */
		}
}
